package com.coco.controller;

import com.coco.mapper.LeaveRequestMapper;
import com.coco.pojo.LeaveRequest;
import com.coco.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * 请假申请管理控制器
 */
@RestController
@RequestMapping("/api/leaveRequests")
public class LeaveRequestController {

    @Autowired
    private LeaveRequestMapper leaveRequestMapper;

    /**
     * 保存或更新请假申请信息
     * @param leaveRequest 请假申请对象
     * @return API响应
     */
    @PostMapping("/saveOrUpdate")
    public ApiResponse<Void> saveOrUpdate(@RequestBody LeaveRequest leaveRequest) {
        if (leaveRequest.getId() == null) {
            leaveRequestMapper.insert(leaveRequest);
        } else {
            leaveRequestMapper.updateById(leaveRequest);
        }
        return ApiResponse.success("操作成功", null);
    }

    /**
     * 删除指定ID的请假申请
     * @param id 请假申请ID
     * @return API响应
     */
    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@PathVariable Integer id) {
        leaveRequestMapper.deleteById(id);
        return ApiResponse.success("删除成功", null);
    }

    /**
     * 批量删除请假申请
     * @param ids 请假申请ID列表
     * @return API响应
     */
    @DeleteMapping("/batch")
    public ApiResponse<Void> deleteBatch(@RequestBody List<Integer> ids) {
        for (Integer id : ids) {
            leaveRequestMapper.deleteById(id);
        }
        return ApiResponse.success("批量删除成功", null);
    }

    /**
     * 查询所有请假申请信息
     * @return API响应
     */
    @GetMapping
    public ApiResponse<List<LeaveRequest>> findAll() {
        List<LeaveRequest> leaveRequests = leaveRequestMapper.selectAll();
        return ApiResponse.success(leaveRequests);
    }

    /**
     * 查询指定ID的请假申请信息
     * @param id 请假申请ID
     * @return API响应
     */
    @GetMapping("/{id}")
    public ApiResponse<LeaveRequest> findOne(@PathVariable Integer id) {
        LeaveRequest leaveRequest = leaveRequestMapper.selectById(id);
        return ApiResponse.success(leaveRequest);
    }

    /**
     * 分页查询请假申请信息
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @param userId 用户ID
     * @param leaveType 请假类型
     * @param status 请假状态
     * @return API响应
     */
    @GetMapping("/page")
    public ApiResponse<PageInfo<LeaveRequest>> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                                        @RequestParam(defaultValue = "10") Integer pageSize,
                                                        @RequestParam(required = false) Integer userId,
                                                        @RequestParam(required = false) String leaveType,
                                                        @RequestParam(required = false) String status) {
        PageHelper.startPage(pageNum, pageSize);
        int offset = (pageNum - 1) * pageSize;
        List<LeaveRequest> leaveRequests = leaveRequestMapper.selectPage(userId, leaveType, status, offset, pageSize);
        int total = leaveRequestMapper.count(userId, leaveType, status);
        PageInfo<LeaveRequest> pageInfo = new PageInfo<>(leaveRequests);
        pageInfo.setTotal(total);
        return ApiResponse.success(pageInfo);
    }

    /**
     * 更新请假申请状态
     * @param id 请假申请ID
     * @param status 新状态
     * @return API响应
     */
    @PutMapping("/{id}/status")
    public ApiResponse<Void> updateStatus(@PathVariable Integer id, @RequestParam String status) {
        leaveRequestMapper.updateStatus(id, status);
        return ApiResponse.success("状态更新成功", null);
    }
}